package stanngpay

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/pay/base"
	"gitee.com/liuxuezhan/ar-platform/pay-channel-packet/stanPaysNg"
	"github.com/spf13/cast"
)

// 充值类型
const (
	InternalDefault = iota
	InternalPay
	InternalLocalEWallet
	InternalPIX
	InternalLocalUsdt
	InternalUSDT
	InternalOnlinePayment
	InternalLocalBank
	InternalPAYTM
	InternalDebitCard
	InternalBankCardQRCode
	InternalLocalUPI
	InternalThirdBankCard
	InternalTRX
	InternalARPay
	InternalNewUPI
	InternalUSDTFB
)

type Factory struct {
}

func (p *Factory) CreatePay(config base.Config) base.Payment {
	return &StanngPay{MerchantId: config.MerchantId, PrivateKey: config.PrivateKey, PublicKey: config.PublicKey, NotifyUrl: config.NotifyUrl, Url: config.Url}
}

type StanngPay struct {
	MerchantId string
	PrivateKey string
	PublicKey  string
	NotifyUrl  string
	Url        string
}

func (s *StanngPay) Pay(ctx context.Context, params map[string]interface{}) (resp base.PayResponse, err error) {
	_method := s.getPaymentMethod(cast.ToInt32(params["rechargeTypeId"]))

	if _tmd, ok := params["method"]; ok {
		_method = cast.ToString(_tmd)
	}
	request := stanPaysNg.PaymentRequest{
		MchOrderNum: cast.ToString(params["orderNo"]),
		Method:      _method,
		Amount:      cast.ToString(params["amount"]),
		NotifyUrl:   s.NotifyUrl,
		CustId:      cast.ToString(params["user_id"]),
		CustName:    "zhangsan",
		CustEmail:   "zhangsan@gmail.com",
	}
	payment, err := stanPaysNg.NewStanPayNg(s.MerchantId).SetPaymentUrl(s.Url).SetPrivateKey(s.PrivateKey).Payment(request)
	if err != nil {
		return
	}
	resp = base.PayResponse{
		Url:     payment.PayData,
		Address: "",
		QrCode:  "",
		OrderNo: payment.PlatOrderNum,
		Result:  payment.ResponseDesc,
	}
	return
}
func (s *StanngPay) getPaymentMethod(rechargeTypeId int32) string {
	_method := stanPaysNg.PaymentMethodVA
	switch rechargeTypeId {
	case InternalARPay:
		_method = stanPaysNg.PaymentMethodVA
	case InternalLocalBank, InternalThirdBankCard:
		_method = stanPaysNg.PaymentMethodCARD
	case InternalBankCardQRCode:
		_method = stanPaysNg.PaymentMethodNQR
	}
	return _method
}
